
import java.util.*;

public class GuyBot {
    // The DoTurn function is where your code goes. The PlanetWars object
    // contains the state of the game, including information about all planets
    // and fleets that currently exist. Inside this function, you issue orders
    // using the pw.IssueOrder() function. For example, to send 10 ships from
    // planet 3 to planet 8, you would say pw.IssueOrder(3, 8, 10).
    //
    // There is already a basic strategy in place here. You can use it as a
    // starting point, or you can throw it out entirely and replace it with
    // your own. Check out the tutorials and articles on the contest website at
    // http://www.ai-contest.com/resources.

    public static void DoTurn(PlanetWars pw, long startTime) {
        //double strengthWeight = 50.0 / pw.NumShips(1);
        double strengthWeight = .01;
        double growthWeight = 30.0;
        double distanceWeight = 10.0;
        int numberOfPlanetsToAttack = 10;
        int totalPlanets = pw.Planets().size();
        int totalMyPlanets = pw.MyPlanets().size();
        int totalEnemyPlanets = pw.EnemyPlanets().size();
        int myShips = pw.NumShips(1);
        int enemyShips = pw.NumShips(2);
        double shipsRatio = (double) myShips / enemyShips;
        double planetsRatio = (double) totalMyPlanets / totalEnemyPlanets;

        numberOfPlanetsToAttack = (int) (shipsRatio + planetsRatio + 2);


        Coord myCenter = pw.CenterOfPlanets(pw.MyPlanets());
        Coord enemyCenter = pw.CenterOfPlanets(pw.EnemyPlanets());

        //reinforcement phase
        List<Planet> myPlanetsByValue = pw.PlanetsSortedByValue(myCenter, pw.MyPlanets(), distanceWeight, growthWeight, strengthWeight);
        for (Planet dest : myPlanetsByValue) {
            List<Planet> myPlanetsByDistance = pw.PlanetsSortedByDistance(dest, pw.MyPlanets());
            for (Planet source : myPlanetsByDistance) {
                int shipsToSend = (-1) * (pw.PredictedStrength(dest, pw.Distance(dest, source)) - 2);
                //int shipsToSend = Math.abs(pw.PlanetNetStrengthPlusGrowthTimesDistance(p2, p)) + 2;
                //int shipsToSend = Math.abs(pw.PlanetNetStrength(p)) + 2;
                if (shipsToSend > source.NumShips()) {
                    //shipsToSend = (int) ((double)source.NumShips() / totalMyPlanets);
                    shipsToSend = 0;
                }
                //if (shipsToSend > 0 && pw.PredictedStrength(p2, myCenter.Distance(enemyCenter)) > 0) {
                //if (shipsToSend > 0 && pw.PlanetNetStrength(p2) > 0) {
                if (shipsToSend > 0 && source != dest) {
                    if (pw.PlanetNetStrength(dest) < 1) {
                        pw.CacheOrder(source, dest, shipsToSend);
                    }
                }
            }
        }
        //attack phase
        List<Planet> planetsByValue = pw.PlanetsSortedByValue(myCenter, pw.NotMyPlanets(), distanceWeight, growthWeight, strengthWeight);
        List<Planet> firstNPlanetsByValue = pw.FirstNPlanets(planetsByValue, numberOfPlanetsToAttack);
        for (Planet p : firstNPlanetsByValue) {
            List<Planet> myPlanetsByDistance = pw.PlanetsSortedByDistance(p, pw.MyPlanets());
            for (Planet p2 : myPlanetsByDistance) {
                int shipsToSend = (-1) * (pw.PredictedStrength(p, pw.Distance(p, p2)) - 2);
                //int shipsToSend = Math.abs(pw.PlanetNetStrengthPlusGrowthTimesDistance(p2, p)) + 2;
                //int shipsToSend = Math.abs(pw.PlanetNetStrength(p)) + 2;
                if (shipsToSend > p2.NumShips()) {
                    //shipsToSend = (int) ((double)p2.NumShips() / totalMyPlanets);
                    shipsToSend = 0;
                }
                //if (shipsToSend > 0 && pw.PredictedStrength(p2, myCenter.Distance(enemyCenter)) > 0) {
                //if (shipsToSend > 0 && pw.PlanetNetStrength(p2) > 0) {
                if (shipsToSend > 0 && p2 != p) {
                    if (p.Owner() != 1 && pw.PlanetNetStrength(p) < 1) {
                        pw.CacheOrder(p2, p, shipsToSend);
                    }
                }
            }
        }

    }

    public static void main(String[] args) {
        PlanetWars pw = new PlanetWars();
        String line = "";
        String message = "";
        int c;
        try {
            while ((c = System.in.read()) >= 0) {
                switch (c) {
                    case '\n':
                        if (line.equals("go")) {
                            long startTime = System.nanoTime();
                            pw.ParseGameState(message);
                            DoTurn(pw, startTime);
                            pw.IssueCachedOrders();
                            pw.FinishTurn();
                            message = "";
                        } else {
                            message += line + "\n";
                        }
                        line = "";
                        break;
                    default:
                        line += (char) c;
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.out);
            // Owned.
        }
    }
}
